package com.dxyxx.framework;

import org.apache.http.Header;
import org.codehaus.jackson.type.JavaType;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import br.com.pierry.simpletoast.SimpleToast;

import com.dxyxx.R;
import com.dxyxx.android.setting.UserPreferences;
import com.dxyxx.dialog.AgainPlayer;
import com.dxyxx.pojo.ResponseResult;
import com.http.HttpUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class ExitFromSettings extends BaseActivity {
	private LinearLayout layout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_logout);
		getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);// 必须加上个，不然就会左右有空隙
		// dialog=new MyDialog(this);
		/*
		 * layout = (LinearLayout) findViewById(R.id.exit_layout2);
		 * layout.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) {
		 * Toast.makeText(getApplicationContext(), "提示：点击窗口外部关闭窗口！",
		 * Toast.LENGTH_SHORT).show();
		 * 
		 * } });
		 */
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finish();
		return true;
	}

	public void exitbutton1(View v) {
		this.finish();
	}

	public void exitbutton0(View v) {
		// 退出登录。 访问http://123.56.91.1:8080/chinesejie/user/logout 
		// 得到 {"code":1,"info":"initial","object":null}
		String urlString = "user/logout";
		HttpUtil.get(urlString, new AsyncHttpResponseHandler() {

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				AgainPlayer.makeCall(ExitFromSettings.this, "网络好像出错了");
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				try {
					JavaType javaType = getObjectMapper().getTypeFactory().constructParametricType(ResponseResult.class, String.class);
					ResponseResult<String> rr = getObjectMapper().readValue(arg2, javaType);

					//{"object":null,"code":1,"info":"验证码已发送，稍等"}
					if (rr.getCode() == 1) { //成功
						// 启动 倒时计数器
						SimpleToast.ok(ExitFromSettings.this, "退出成功");
					} else {
						SimpleToast.ok(ExitFromSettings.this, "没登陆");
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		});
		UserPreferences.getInstance(getApplicationContext()).clearUser();

		Intent intent = new Intent();
		// 关闭Activity
		intent.putExtra("result", "sure");

		ExitFromSettings.this.setResult(ResultCode.Logout, intent);
		ExitFromSettings.this.finish();

	}
}
